package xbook

import (
	"time"

	"gitee.com/xiaoyutab/xgotool/optional/xlog"
	"gitee.com/xiaoyutab/xgotool/xerror"
)

// 创建书籍信息
//
//	c	书籍的结构信息
func Create(c *Book) error {
	if c.UserId == 0 {
		return xerror.New("创建人不能为空")
	}
	if c.Title == "" {
		return xerror.New("书籍标题不能为空")
	}
	if c.CreatedAt == "" {
		c.CreatedAt = time.Now().Format(time.DateTime)
	}
	c.UpdatedAt = time.Now().Format(time.DateTime)
	err := _default.DB.Create(c).Error
	if err != nil {
		return xlog.AE("书籍插入失败", err)
	}
	return nil
}
